import Vue from 'vue';
import Router from 'vue-router';
import Cookies from 'js-cookie'
import util from '@/libs/util'
//push 
const VueRouterPush = Router.prototype.push 
Router.prototype.push = function push (to) {
    return VueRouterPush.call(this, to).catch(err => err)
}

//replace
const VueRouterReplace = Router.prototype.replace
Router.prototype.replace = function replace (to) {
  return VueRouterReplace.call(this, to).catch(err => err)
}

Vue.use(Router);



const router = new Router({
    routes: [
        {
            path: '/',
            component: () => import('../components/common/Home.vue'),
            meta: { title: '自述文件' },
            children: [
                {
                    path: '/dashboard',
                    component: () => import('../components/page/Dashboard.vue'),
                    meta: { title: '首页' ,app:"control"},
                },
                {
                    path: '/404',
                    component: () => import('../components/page/404.vue'),
                    meta: { title: '404' ,app:"control"}
                },
                {
                    path: '/403',
                    component: () => import('../components/page/403.vue'),
                    meta: { title: '403',app:"control" }
                },
                /**中控开始 */
                {
                    path: '/company/search',
                    component: () => import('../views/control/company/companyList.vue'),
                    meta: { title: '企业列表' ,app:"control"}
                }, {
                    path: '/company/add',
                    component: () => import( '../views/control/company/companyAdd.vue'),
                    meta: { title: '添加/编辑企业' ,app:"control"}
                }, {
                    path: '/company/exmined',
                    component: () => import( '../views/control/test/treetable.vue'),
                    meta: { title: '企业审核' ,app:"control"}
                },{
                    path: '/company/view',
                    component: () => import( '../views/control/company/companyView.vue'),
                    meta: { title: '企业查看' ,app:"control"}
                },{
                    path: '/company/resources',
                    component: () => import( '../views/control/company/companyResourceList.vue'),
                    meta: { title: '企业功能' ,app:"control"}
                }, {
                    path: '/app/list',
                    component: () => import( '../views/control/security/TraceApp.vue'),
                    meta: { title: '应用列表',app:"control" }
                }, {
                    path: '/app/roles',
                    component: () => import( '../views/control/security/ResourceList.vue'),
                    meta: { title: '应用功能',app:"control"}
                } ,
                /**中控结束 */
                /**种植开始 */
                {
                    path: '/plant/test',
                    component: () => import( '../views/plant/farm/dataMap.vue'),
                    meta: { title: '数字地图',app:"plant"}
                } ,  {
                    path: '/plant/farm/list',
                    component: () => import( '../views/plant/farm/farmList.vue'),
                    meta: { title: '地块管理',app:"plant"}
                } , {
                    path: '/plant/farm/add',
                    component: () => import( '../views/plant/farm/dataMap.vue'),
                    meta: { title: '添加/编辑地图',app:"plant"}
                } ,{
                    path: '/plant/plant/list',
                    component: () => import( '../views/plant/plant/plantList.vue'),
                    meta: { title: '种植管理',app:"plant"}
                } ,{
                    path: '/plant/plant/add',
                    component: () => import( '../views/plant/plant/plantAdd.vue'),
                    meta: { title: '添加编辑种植',app:"plant"}
                } ,
            ]
        },
        {
            path: '/login',
            component: () => import(/* webpackChunkName: "login" */ '../components/page/Login.vue'),
            meta: { title: '登录' }
        },
        {
            path: '*',
            redirect: '/404'
        }
    ]
});


//使用钩子函数对路由进行权限跳转
router.beforeEach((to, from, next) => {
    let userInfo = util.get("userInfo");
    if(!userInfo){
        if(to.path == '/login'){
            next();
        }else{
            next('/login');
        }
       
    }
    document.title = `模板`;
    const role = util.getCookie('userInfo');
    if (!role && to.path !== '/login') {
        next('/login');
    } else if (to.meta.permission) {
        // 如果是管理员权限则可进入，这里只是简单的模拟管理员权限而已
        role === 'admin' ? next() : next('/403');
    } else {
        // 简单的判断IE10及以下不进入富文本编辑器，该组件不兼容
        if (navigator.userAgent.indexOf('MSIE') > -1 && to.path === '/editor') {
            Vue.prototype.$alert('vue-quill-editor组件不兼容IE10及以下浏览器，请使用更高版本的浏览器查看', '浏览器不兼容通知', {
                confirmButtonText: '确定'
            });
        } else {
            next();
        }
    }
});

export default router;